package com.cs.statistic;

import android.content.Context;
import android.text.TextUtils;

import com.cs.bd.ad.self.AdIdHelper;
import com.cs.bd.utils.LogUtils;
import com.cs.statistic.beans.OptionBean;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;

import java.util.HashMap;
import java.util.Map;

public class StatisticsManager {

    private static String channel;

    private static Context context;

    private static StatisticsManager sInstance;

    public static synchronized StatisticsManager getInstance(Context context) {
        StatisticsManager.context = context;
        if (null == sInstance) {
            sInstance = new StatisticsManager();
            UMConfigure.init(context, AdIdHelper.getStatisticsAppKey(context), channel, UMConfigure.DEVICE_TYPE_PHONE, null);
        }
        return sInstance;
    }

    public void onResume(Context context) {
        try {
            MobclickAgent.onResume(context);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void onPause(Context context) {
        try {
            MobclickAgent.onPause(context);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void initBasicInfo(String mainProcessName, String channel, String IMEI, String authority) {
        StatisticsManager.channel = channel;
    }

    public static void reportError(Context var0, String var1) {
        MobclickAgent.reportError(var0, var1);
    }

    public void enableLog(boolean onOff) {

    }

    public void upLoadStaticData(String staticData) {
        LogUtils.d(staticData);
    }

    public void uploadStaticData(int logId, int funid, String buffer) {
        try {
            if (!TextUtils.isEmpty(buffer)) {
                String opt = null;
                Map<String, String> map = new HashMap<>();
                String []arrays = buffer.split("\\|\\|");
                for (int i = 0; i < arrays.length; i++) {
                    String tmp = arrays[i];
                    if (!TextUtils.isEmpty(tmp)) {
                        switch (i) {
                            case 2:
                                opt = tmp;
                                break;
                            case 3:
                                map.put("entrance", tmp);
                                break;
                            case 4:
                                map.put("tab", tmp);
                                break;
                            case 5:
                                map.put("location", tmp);
                                break;
                            case 6:
                                map.put("associations", tmp);
                                break;
                            case 7:
                                map.put("note", tmp);
                                break;
                        }
                    }
                }
                if (!TextUtils.isEmpty(opt)) {
                    if (map.isEmpty()) {
                        MobclickAgent.onEvent(context, opt);
                    } else {
                        MobclickAgent.onEvent(context, opt, map);
                    }
                    LogUtils.d("opt : " + opt + ", size : " + map.size());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void upLoadBasicInfoStaticData(String channel, boolean isPay, boolean needRootInfo, String key, boolean isNew, String appendData) {
        try {
            Map<String, String> map = new HashMap<>();
            map.put("channel", channel);
            map.put("isPay", isPay + "");
            map.put("needRootInfo", needRootInfo + "");
            map.put("key", key);
            map.put("isNew", isNew + "");
            map.put("appendData", appendData);
            MobclickAgent.onEvent(context, "19", map);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void uploadStaticDataForOptions(int logId, int funid, String buffer, OnInsertDBListener insertDBListener, OptionBean... options) {
        uploadStaticData(logId,funid, buffer);
    }

    public static String getUserId(Context context) {
        return "11";
    }

}
